package org.hxzon.cyclone;

//particle.h
//particle.cpp
//粒子
public class Particle {
    private double inverseMass;//逆质量，即质量的倒数
    private Vector3 position;//位置
    private Vector3 velocity;//速度
    private Vector3 acceleration;//加速度
    private Vector3 forceAccum;//合力
    private double damping;//阻尼

    //积分计算
    //duration,时长
    public void integrate(double duration) {
        // We don't integrate things with zero mass.
        if (inverseMass <= 0.0f) {
            return;
        }

        assert (duration > 0.0);

        // 更新位置
        position.addScaledVector(velocity, duration);

        // 更新加速度
        Vector3 resultingAcc = acceleration.copy();
        resultingAcc.addScaledVector(forceAccum, inverseMass);

        // 更新速度
        velocity.addScaledVector(resultingAcc, duration);

        // Impose drag.
        velocity.multiUpdate(Util.realPow(damping, duration));

        // Clear the forces.
        clearAccumulator();
    }

    public void setMass(double mass) {
        assert (mass != 0);
        inverseMass = ((double) 1.0) / mass;
    }

    public double getMass() {
        if (inverseMass == 0) {
            return Util.RealMax;
        } else {
            return ((double) 1.0) / inverseMass;
        }
    }

    public void setInverseMass(double inverseMass) {
        this.inverseMass = inverseMass;
    }

    public double getInverseMass() {
        return inverseMass;
    }

    public boolean hasFiniteMass() {
        return inverseMass >= 0.0f;
    }

    public void setDamping(double damping) {
        this.damping = damping;
    }

    public double getDamping() {
        return damping;
    }

    public void setPosition(Vector3 position) {
        this.position = position;
    }

    public void setPosition(double x, double y, double z) {
        position.x = x;
        position.y = y;
        position.z = z;
    }

    public Vector3 getPosition() {
        return position;
    }

    public void setVelocity(Vector3 velocity) {
        this.velocity = velocity;
    }

    public void setVelocity(double x, double y, double z) {
        velocity.x = x;
        velocity.y = y;
        velocity.z = z;
    }

    public Vector3 getVelocity() {
        return velocity;
    }

    public void setAcceleration(Vector3 acceleration) {
        this.acceleration = acceleration;
    }

    public void setAcceleration(double x, double y, double z) {
        acceleration.x = x;
        acceleration.y = y;
        acceleration.z = z;
    }

    public Vector3 getAcceleration() {
        return acceleration;
    }

    public void clearAccumulator() {
        forceAccum.clear();
    }

    public void addForce(Vector3 force) {
        forceAccum.addUpdate(force);
    }
}
